package weaver.interfaces.cyjt.duss.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import weaver.general.Util;

import java.util.concurrent.TimeUnit;

/**
 * @author dusongsong
 * @date 2024年03月28日 16:45
 */
public class CyjtActionUtil {
    private static final String URL = "https://a8.fway.vip/prod-api/task/dataSaveFromOA";

    /**
     * 发送请求
     *
     * @param params
     * @return
     */
    public static JSONObject sendOKHttpRequest(String params) {
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params);
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

}
